package com.by.wx.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.by.common.conf.CommandMager;
import com.by.common.conf.WxConfMager;
import com.by.common.cont.CommandConst;
import com.by.common.util.HttpclientUtils;
import com.by.common.vo.JsonResult;
import com.by.wx.entity.SysEquipmentOperate;
import com.by.wx.entity.SysRoomEquipment;
import com.by.wx.service.RoomEquipService;
import com.by.wx.vo.BackParamJsonResult;

@Controller
@RequestMapping("/cotol/")
public class RoomEquipController {

	public static Log log = LogFactory.getLog(PhoneController.class);

	@Autowired
	private RoomEquipService roomEquipService;

	@RequestMapping("wxroomconl")
	public String wxroomconl() {
		// //local
		// return "wxin/room_col";
		// cluster
		return "wxin/wx_smart";
	}

	@RequestMapping("wxIllageArgument")
	public String wxIllageArgument() {
		return "wxin/illegal_argument_http_address";
	}

	// all
	@RequestMapping("doAllOperate")
	public String doAllOperate(String renterId, String roomId, String timestamp, String sign, HttpSession session) {
		return "wxin/command_room_all";
	}

	// backup-info
	@RequestMapping("doAutoLockOperate")
	public String doAutoLockOperate(String renterId, String roomId, String timestamp, String sign,
			HttpSession session) {
		return "wxin/command_room_autoLock";
	}

	// backup-info
	@RequestMapping("doSwitchOperate")
	public String doSwitchOperate(String renterId, String roomId, String timestamp, String sign, HttpSession session) {
		return "wxin/command_room_switch";
	}

	// backup-info
	@RequestMapping("doCurtainOperate")
	public String doCurtainOperate(String renterId, String roomId, String timestamp, String sign, HttpSession session) {
		return "wxin/command_room_curtain";
	}

	// backup-info
	@RequestMapping("doInfraredForwardOperate")
	public String doInfraredForwardOperate(String renterId, String roomId, String timestamp, String sign,
			HttpSession session) {
		return "wxin/command_room_infraredForward";
	}

	private int verifyHttpAddressParamters2OperateRoomDeviced(String renterId, String roomId, String timestamp,
			String sign, HttpSession session) {
		String sessionSign = (String) session.getAttribute("sign");
		String rid = (String) session.getAttribute("room_id");
		String vid = String.valueOf((Integer) session.getAttribute("renter_id"));
		String sessionTime = (String) session.getAttribute("timestamp");
		// System.out.println("#####"+sessionSign);
		// System.out.println("#####"+rid);
		// System.out.println("#####"+vid);
		// System.out.println("#####"+sessionTime);
		// System.out.println("--------------------");
		// System.out.println(renterId);
		// System.out.println(roomId);
		// System.out.println(sign);
		// System.out.println(timestamp);
		// System.out.println("--------------------");
		if (rid.equals(roomId) && vid.equals(renterId) && sessionSign.equals(sign) && sessionTime.equals(timestamp)) {
			return 1;
		} else {
			System.out.println("----------fail------------");
			return 0;
		}
	}

	@RequestMapping("verifyRoomControlPage")
	@ResponseBody
	public JsonResult verifyRoomControlPage(String renterId, String roomId, String timestamp, String sign,
			HttpSession session) {
		int result = verifyHttpAddressParamters2OperateRoomDeviced(renterId, roomId, timestamp, sign, session);
		if (result == 1) {
			// System.out.println("跳转成功");
			return new JsonResult(1, "跳转成功", true);
		} else if (result == 0) {
			System.err.println("----------fail------------");
			return new JsonResult(0, "输入非法参数", false);
		} else {
			// System.out.println("---55555--");
			return new JsonResult(5, "未知错误", false);
		}
	}

	@RequestMapping("findEquipmentOperateByCode")
	@ResponseBody
	public JsonResult findEquipmentOperateByCode(String dt) {
		List<SysEquipmentOperate> entity = roomEquipService.findEquipmentOperateByCode(dt);
		return new JsonResult(1, "action ok", entity);
	}

	@RequestMapping("handleRoom")
	@ResponseBody
	public JsonResult handleRoom(String a, String b, String c, String d, String e, String f, String g, String h) {
		// String url =
		// "http://localhost:8765/device/deviceControl?token=1&deviceToken=4394e822-4243-4f30-9f2d-1b53c356d142&device=4810053E&type=InfraredForward&control=o&panelIndex=0&buttonIndex=1&password=1";
		// System.out.println("b="+b);
		String codeVal = null;
		String descVal = null;
		// String timeVal = null;
		// String dataVal= null;

		String[] params = getGetBackParams2VerifyCommandUrl(a, b, c, d, e, f, g, h);
		for (int i = 0; i < params.length; i++) {
			if ("code".equals(params[i].split(":")[0])) {
				codeVal = params[i].split(":")[1];
			} else if ("desc".equals(params[i].split(":")[0])) {
				descVal = params[i].split(":")[1];
			}
			// else if("time".equals(params[i].split(":")[0])){
			// timeVal = params[i].split(":")[1];
			// } else if("data".equals(params[i].split(":")[0])){
			// dataVal = params[i].split(":")[1];
			// }
		}

		if ("200".equals(codeVal)) {
			return new JsonResult(1, "操作成功", true);
		}
		return new JsonResult(0, descVal, false);
	}
	
	@RequestMapping("setRooPwd")
	@ResponseBody
	public JsonResult setRooPwd(String a, String b, String c, String d, String e, String f, String g, String h){
//		System.out.println(h);
		String codeVal = null;
		String descVal = null;
		// String timeVal = null;
		String dataVal= null;
		String[] params = getGetBackParams2VerifyCommandUrl(a, b, c, d, e, f, g, h);
		for (int i = 0; i < params.length; i++) {
			if ("code".equals(params[i].split(":")[0])) {
				codeVal = params[i].split(":")[1];
			} else if ("desc".equals(params[i].split(":")[0])) {
				descVal = params[i].split(":")[1];
			}
			else if("data".equals(params[i].split(":")[0])){
				 dataVal = params[i].split(":")[1];
			}
			// else if("time".equals(params[i].split(":")[0])){
			// timeVal = params[i].split(":")[1];
			// } 
		}
		if ("200".equals(codeVal)) {
			return new JsonResult(1, descVal, true);
		}
		
		return new JsonResult(0 , dataVal ,false);
	}

	private String[] getGetBackParams2VerifyCommandUrl(String token, String deviceToken, String device, String type,
			String control, String panelIndex, String buttonIndex, String password) {
		String propUrl = "";
		if (CommandMager.getBoolean(CommandConst.WX_ONTROL_ROOM_TEST_LOCAL)) {
			propUrl = CommandMager.getProperty(CommandConst.WX_CONTROL_ROOM_EQUIPMENT_REDIRECT_URL_PREF_LOCAL);
		} else {
			propUrl = CommandMager.getProperty(CommandConst.WX_CONTROL_ROOM_EQUIPMENT_REDIRECT_URL_PREF_CLUSTER);
		}
		propUrl = propUrl + ":" +CommandMager.getProperty(CommandConst.WX_CONTROL_ROOM_EQUIPMENT_REDIRECT_URL_PROT) + CommandMager.getProperty(CommandConst.WX_CONTROL_ROOM_EQUIPMENT_REDIRECT_URL_METHOD);
		String url = propUrl + "?token="+token+"&deviceToken="+deviceToken+"&device="+device+"&type="+type+
				"&control="+control+"&panelIndex="+panelIndex+"&buttonIndex="+buttonIndex+"&password="+password;
//		System.out.println(url);
		String result = HttpclientUtils.getGetDateByUrl(url).replace("{", "").replace("}", "").replace("\"", "");
		String[] res = result.split(",");
		// System.out.println(url);
		return res;
	}
	
	
	// TODO test
	@RequestMapping("findRoomEquipmentByUser")
	@ResponseBody
	public JsonResult findRoomEquipmentByUser(String roomNumber) {
		List<SysRoomEquipment> entity = roomEquipService.findRoomEquipmentByUser(roomNumber);
		return new JsonResult(1, "查询成功", entity);
	}
		
}
